package com.b.btest;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListArtistTrackAdapter extends ArrayAdapter<ArtistTrack> {

	LayoutInflater mInflater;

	public ListArtistTrackAdapter(Context context, List<ArtistTrack> item) {
		super(context, 0, item);

		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ArtistTrack item = getItem(position);
		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_track, null);
			holder = new ViewHolder();
			holder.trackTextView = (TextView) convertView.findViewById(R.id.title);
			holder.artistTextView = (TextView) convertView.findViewById(R.id.artist);
			holder.durationTextView = (TextView) convertView.findViewById(R.id.duration);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		long dm = item.duration / 60000;
		long ds = (item.duration - (dm * 60000)) / 1000;

		holder.artistTextView.setText(item.artist);
		holder.trackTextView.setText(item.title);
		holder.durationTextView.setText(String.format("%d:%02d", dm, ds));

		if (position % 2 == 0) {
			convertView.setBackgroundColor(Color.argb(50, 40, 200, 150));
		}
		else {
			convertView.setBackgroundColor(Color.argb(50, 250, 250, 250));
		}

		return convertView;
	}

	static class ViewHolder {
		TextView trackTextView;
		TextView artistTextView;
		TextView durationTextView;
	}

}